define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'rexam/question/index' + location.search,
                    add_url: 'rexam/question/add',
                    edit_url: 'rexam/question/edit',
                    del_url: 'rexam/question/del',
                    multi_url: 'rexam/question/multi',
                    import_url: 'rexam/question/import',
                    table: 'rexam_question',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'subject.name', title: __('Rexam_subject_id')},
                        {field: 'typedata', title: __('Typedata'), searchList: {"1":__('Typedata 1'),"2":__('Typedata 2'),"3":__('Typedata 3'),"4":__('Typedata 4')}, formatter: Table.api.formatter.normal},
                        {field: 'title', title: __('Title'), formatter: Table.api.formatter.content},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            typeChange();
            $("input:radio[name='row[typedata]']").click(function(){
                typeChange();
            })
            $(" #gChoice").on('click', "input:checkbox", function(){
                var type = $("input:radio[name='row[typedata]']:checked").val();
                var name = $(this).attr('name');
                // 单选题
                if(type == 1){
                    $("#gChoice input:checkbox").each(function(){
                        if($(this).attr('name')!==name){
                            $(this).attr("checked", false);
                        }
                    });
                }
            })
        },
        edit: function () {
            Controller.api.bindevent();
            typeChange();
            $("input:radio[name='row[typedata]']").click(function(){
                typeChange();
            })
            $(" #gChoice").on('click', "input:checkbox", function(){
                var type = $("input:radio[name='row[typedata]']:checked").val();
                var name = $(this).attr('name');
                // 单选题
                if(type == 1){
                    $("#gChoice input:checkbox").each(function(){
                        if($(this).attr('name')!==name){
                            $(this).attr("checked", false);
                        }
                    });
                }
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    function typeChange(){
        var type = $("input:radio[name='row[typedata]']:checked").val();
        if(type == 3){
            $("#gChoice").hide();
            $("#gJudge").show();
        }else{
            $("#gChoice").show();
            $("#gJudge").hide();
        }
    }
    return Controller;
});